package com.handa.admin.webChat.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.handa.admin.userservice.common.entity.Response;
import com.handa.admin.webChat.entity.ChatMessageDO;
import com.handa.admin.webChat.service.ChatMessageService;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;
import java.util.Objects;

/**
 * (ChatMessageDO)表控制层
 *
 * @author makejava
 * @since 2025-11-06 14:21:21
 */
@RestController
@RequestMapping("/Admin/chatMessage")
public class ChatMessageController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private ChatMessageService chatMessageService;


    @GetMapping
    public Response<?> selectAll() {
        List<ChatMessageDO> list = chatMessageService.list();
        return Response.success(list);
    }


    @PostMapping("select")
    public Response<?> select(@RequestBody ChatMessageDO chatMessageDO) {
        QueryWrapper<ChatMessageDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(chatMessageDO.getSender()), "sender", chatMessageDO.getSender());
        queryWrapper.eq(StringUtils.isNotBlank(chatMessageDO.getReceiver()), "receiver", chatMessageDO.getReceiver());
        queryWrapper.eq(!Objects.isNull(chatMessageDO.getMessageType()), "message_type", chatMessageDO.getMessageType());
        List<ChatMessageDO> list = chatMessageService.list(queryWrapper);
        return Response.success(list);
    }

    @PostMapping("selectByBothUser")
    public Response<?> selectByBothUser(@RequestBody ChatMessageDO chatMessageDO) {
        QueryWrapper<ChatMessageDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.and(wrapper -> wrapper
                        .eq(StringUtils.isNotBlank(chatMessageDO.getSender()), "sender", chatMessageDO.getSender())
                        .eq(StringUtils.isNotBlank(chatMessageDO.getReceiver()), "receiver", chatMessageDO.getReceiver())
                ).or(wrapper -> wrapper
                        .eq(StringUtils.isNotBlank(chatMessageDO.getReceiver()), "sender", chatMessageDO.getReceiver())
                        .eq(StringUtils.isNotBlank(chatMessageDO.getSender()), "receiver", chatMessageDO.getSender())
                )
                .orderByAsc("created_at"); // 按创建时间升序排序
        List<ChatMessageDO> list = chatMessageService.list(queryWrapper);
        return Response.success(list);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("getById/{id}")
    public Response<?> selectOne(@PathVariable Serializable id) {
        return Response.success(this.chatMessageService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param chatMessageDO 实体对象
     * @return 新增结果
     */
    @PostMapping("insert")
    public Response<?> insert(@RequestBody ChatMessageDO chatMessageDO) {
        return Response.success(this.chatMessageService.save(chatMessageDO));
    }

    /**
     * 修改数据
     *
     * @param chatMessageDO 实体对象
     * @return 修改结果
     */
    @PostMapping("update")
    public Response<?> update(@RequestBody ChatMessageDO chatMessageDO) {
        return Response.success(this.chatMessageService.updateById(chatMessageDO));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @PostMapping("delete")
    public Response<?> delete(@RequestBody List<Long> idList) {
        return Response.success(this.chatMessageService.removeByIds(idList));
    }
}

